﻿using GalaSoft.MvvmLight.Messaging;
using NicoView.Model.Service;
using NicoView.Model.Service.Lives;
using NicoView.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using NicoView.Extentions;

using NicoView.ViewModel;
using System.Windows.Automation.Peers;
using System.Windows.Automation;
using System.Diagnostics;
using NicoView.View.CommentScroll;
using System.Windows.Threading;
using System.Reflection;
using NicoView.ViewModel.Message;


namespace NicoView.View
{
    /// <summary>
    /// NicoCommentViewControl.xaml の相互作用ロジック
    /// </summary>
    public partial class NicoViewCommentViewControl : UserControl
    {
        public NicoViewCommentViewControl()
        {

            InitializeComponent();

            //VirtualizingStackPanel.SetVirtualizationMode(this.CommentListBox, VirtualizationMode.Recycling);
        }







        private CommentScrollContext _scrollContext;

        private ScrollViewer _scrollViewer;



        public new NicoViewCommentListViewModelBase DataContext
        {
            get
            {
                return (NicoViewCommentListViewModelBase)base.DataContext;
            }

            set
            {
                base.DataContext = value;
               
                

            }

        }



        private void _scrollRequest(Action<CommentScrollContext> contextRequest)
        {
            if (_scrollContext == null) return;
            contextRequest(_scrollContext);
        }



        private  void CommentScrollViwer_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {

            
            

                _scrollRequest((c) =>
                {
                    c.OnScrollChanged(e, this.CommentListBox.Items.Count);
                });

                    
        }

        private void CommentScrollViwer_Loaded(object sender, RoutedEventArgs e)
        {
            
            _scrollViewer = (ScrollViewer)sender;
            this.DataContext.Loded();
           
            
         
        }

        private void CommentListViewControl_Loaded(object sender, RoutedEventArgs e)
        {
            _scrollContext = null;
            if (!DataContext.IsInDesignMode) Messenger.Default.Register<NicoViewMainCommentListViewModelLiveCommentClientMessage>(this, (args) =>
            {
                _scrollContext = new LiveCommentScrollContext(DataContext, _scrollViewer);

            });

        }

        private void CommentListBox_LayoutUpdated(object sender, EventArgs e)
        {
            /*
            _scrollRequest((c) =>
            {
                c.OnLayoutUpdated(e, DataContext.Comments.Count);
            });
             */
        }

        private void PART_VerticalScrollBar_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
        {
            _scrollRequest((c) =>
            {
                c.OnScroll(e, this.CommentListBox.Items.Count);
            });

        }





        private void Grid_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            _scrollRequest((c) =>
            {
                c.OnMouseWheel(e, this.CommentListBox.Items.Count);
            });
        }

        private void repeatButton_Click(object sender, RoutedEventArgs e)
        {
            _scrollRequest((c) =>
            {
                c.OnLineUpClick(e, this.CommentListBox.Items.Count);
            });
        }

        private void CommentListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _scrollRequest((c) =>
            {
                c.OnSelectionChange(e,this.CommentListBox.Items,  this.CommentListBox.Items.Count);
            });
        }

        private void CommentContent_MouseDown(object sender, MouseButtonEventArgs e)
        {


            _scrollRequest((c) =>
            {
                c.OnRichTextMouseDown(e, this.CommentListBox.Items, this.CommentListBox.Items.Count);
            });
        }

        private void CommentViewSource_Filter(object sender, FilterEventArgs e)
        {
            var comment = e.Item as NicoViewCommentViewModel;
            if (comment != null)
            {
                e.Accepted = this.DataContext.IsCommentAccepted(comment);

            }

        }

   
    }
}
